<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="infomationList" @selection-change="handleSelectionChange">
      <el-table-column label="单位名称" align="center" prop="name">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.train_institution" :value="scope.row.name"/>
        </template>
      </el-table-column>
      <el-table-column label="公司地址" align="center" prop="address" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="申请人" align="center" prop="contactPerson" />
      <el-table-column label="单位负责人（签字）" align="center" prop="unitHeader" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.unitHeader" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="单位经办人（签字）" align="center" prop="unitManager" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.unitManager" :width="50" :height="50"/>
        </template>
      </el-table-column>
      
      <el-table-column label="公章" align="center" prop="applicationUnitSheet" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.applicationUnitSheet" :width="50" :height="50"/>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            style="width: 40px;"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['common:infomation:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改培训机构基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="单位名称" prop="name">
          <el-select v-model="form.name" placeholder="请选择单位名称">
            <el-option
              v-for="dict in dict.type.train_institution"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入公司地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="申请人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="单位负责人（签字）" prop="unitHeader">
          <image-upload v-model="form.unitHeader"/>
        </el-form-item>
        <el-form-item label="单位经办人（签字）" prop="unitManager">
          <image-upload v-model="form.unitManager"/>
        </el-form-item>
        <el-form-item label="公章" prop="applicationUnitSheet">
          <image-upload v-model="form.applicationUnitSheet"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfomation, getInfomation, delInfomation, addInfomation, updateInfomation } from "@/api/common/infomation";

export default {
  name: "Infomation",
  dicts: ['exam_place', 'train_institution'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训机构基本信息表格数据
      infomationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训机构基本信息列表 */
    getList() {
      this.loading = true;
      listInfomation(this.queryParams).then(response => {
        this.infomationList = response.rows;
        
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        address: null,
        contactPhone: null,
        contactPerson: null,
        isExamPlace: null,
        examPlace: null,
        isDeleted: null,
        createId: null,
        updateId: null,
        createTime: null,
        updateTime: null,
        extend: null,
        applicationUnitSheet: null,
        unitHeader: null,
        unitManager: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训机构基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfomation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改培训机构基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfomation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfomation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训机构基本信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfomation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('common/infomation/export', {
        ...this.queryParams
      }, `infomation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
